<template>
  <upload-excel :on-success="handleSuccess" />
</template>

<script>
import { importEmployee } from '@/api/employees'
import { formatExcelDate } from '@/utils'
export default {
  name: 'Import',

  methods: {
    // 提交的接口
    async employees(arr) {
      const res = await importEmployee(arr)
      console.log(res)
    },
    transExcel(results) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '部门': 'departmentName',
        '聘用形式': 'formOfEmployment'
      }
      return results.map((item) => {
        // console.log(item)
        const obj = {}
        const zhKey = Object.keys(item)
        zhKey.forEach(abc => {
        //   console.log(abc)
          const enKey = userRelations[abc]
          //   console.log(enKey)
          //   console.log(item)
          //   console.log(abc)
          //   item[abc] 等于item中对面abc中的每一个值
          if (['timeOfEntry', 'correctionTime'].includes(enKey)) {
            obj[enKey] = new Date(formatExcelDate(item[abc]))
          } else {
            obj[enKey] = item[abc]
          }
        })
        // const enKey = Object.values(userRelations)
        // console.log(enKey)
        return obj
      })
    },
    handleSuccess({ header, results }) { // header 表头   results 内容
      console.log(header, results)
      const arr = this.transExcel(results)
      console.log(arr)
      this.employees(arr)
      // 跳转会上一页
      this.$router.back()
    }

  }
}
</script>
